package com.dzm.xgb.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dzm.common.constants.RedisConstants;
import com.dzm.xgb.core.pojo.dto.ExcelDictDTO;
import com.dzm.xgb.core.listener.MyReadListener;
import com.dzm.xgb.core.pojo.entity.Dict;
import com.dzm.xgb.core.mapper.DictMapper;
import com.dzm.xgb.core.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author Dzm
 * @since 2024-08-27
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void importData(MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        //1.1 根据文件流读取文件数据
        EasyExcel.read(is, ExcelDictDTO.class, new MyReadListener(this)).sheet().doRead();

    }

    @Override
    public List<ExcelDictDTO> listDictData() {
        List<Dict> list = list();

        List<ExcelDictDTO> collect = list.stream().map(obj -> {
            ExcelDictDTO dto = new ExcelDictDTO();
            BeanUtils.copyProperties(obj, dto);
            return dto;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Dict> findChildrenByParentId(Integer pid) {
        List<Dict> list = null;
        list = (List<Dict>) redisTemplate.opsForValue().get(RedisConstants.DICT_CHILDREN_LIST + pid);

        if(list == null || list.size() == 0) {
            LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dict::getParentId, pid);
            list = this.list(wrapper);

            list.forEach(dict -> {
                //判断分类有没有子分类
                boolean hasChildren = hasChildren(dict.getId());
                dict.setHasChildren(hasChildren);
            });

            //数据存入缓存
            redisTemplate.opsForValue().set(RedisConstants.DICT_CHILDREN_LIST + pid, list);
        }

        return list;
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dict::getDictCode, dictCode);
        //获取父字典
        Dict dict = this.getOne(wrapper);
        //获取其子字典
        List<Dict> list = findChildrenByParentId(new Integer(dict.getId().toString()));
        return list;
    }

    private boolean hasChildren(Long id) {
        int count = count(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, id));
        return count > 0;
    }

}
